package com.tzxs.pay.main.installment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.CalendarView;
import android.widget.Toast;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivitySearchIbBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.gather.SearchResultActivity;
import com.tzxs.pay.util.StatusBarColorSetter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 分期账单查询
 */

public class SearchIBillActivity extends BaseActivity<ActivitySearchIbBinding> implements View.OnClickListener {
    private String startTimeStr = "";//开始时间
    private String endTimeStr = "";//结束时间

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_ib;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, Color.parseColor("#FAFAFA"));
        dataBinding.searchIbToolbar.setTitle("");
        setSupportActionBar(dataBinding.searchIbToolbar);
        dataBinding.searchIbToolbar.setNavigationOnClickListener(v -> onBackPressed());
        dataBinding.searchIbBtn.setOnClickListener(this);
        dataBinding.searchStartTime.setOnClickListener(this);
        dataBinding.searchEndTime.setOnClickListener(this);
    }

    /**
     * 选择时间
     *
     * @param id 控件id
     */
    private void chooseTime(final int id) {
        final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
        View content = getLayoutInflater().inflate(R.layout.time_choose, null);
        CalendarView calendarView = content.findViewById(R.id.order_time_calendarView);
        bottomSheetDialog.setContentView(content);
        bottomSheetDialog.show();
        calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) -> {
            bottomSheetDialog.dismiss();
            String mM;
            String day;
            if (dayOfMonth < 10) {//日
                day = "0" + String.valueOf(dayOfMonth);
            } else {
                day = String.valueOf(dayOfMonth);
            }
            if (month < 10) {
                mM = "0" + String.valueOf(month + 1);
            } else {
                mM = String.valueOf(month + 1);
            }
            switch (id) {
                case R.id.search_start_time:
                    startTimeStr = year + "-" + mM + "-" + day;
                    dataBinding.searchStartTime.setText(startTimeStr);
                    break;
                case R.id.search_end_time:
                    endTimeStr = year + "-" + mM + "-" + day;
                    dataBinding.searchEndTime.setText(endTimeStr);
                    break;
            }
        });
    }

    //判断时间段选择
    private long timeJudge(String time) {
        SimpleDateFormat simpleDateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
        simpleDateFormat.applyPattern("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_ib_btn://搜索
                if (TextUtils.isEmpty(dataBinding.searchIbNum.getText().toString()) &&
                        TextUtils.isEmpty(startTimeStr) && TextUtils.isEmpty(endTimeStr)) {
                    Toast.makeText(this, "请输入至少一个搜索条件", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (timeJudge(endTimeStr) < timeJudge(startTimeStr)) {
                    Toast.makeText(this, "结束时间必须小于开始时间", Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent(this, SearchResultActivity.class);
                intent.putExtra("orderId", dataBinding.searchIbNum.getText().toString());
                intent.putExtra("startTime", startTimeStr);
                intent.putExtra("endTime", endTimeStr);
                intent.putExtra("name", "install");
                startActivity(intent);
                break;
            //选择开始时间
            case R.id.search_start_time:
                chooseTime(R.id.search_start_time);
                break;
            //选择结束时间
            case R.id.search_end_time:
                chooseTime(R.id.search_end_time);
                break;
        }
    }
}
